package com.manage.util;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.processing.OperationManager;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.manage.common.GlobalVariableNames;
import com.manage.context.GlobalContext;
import org.springframework.ui.Model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author jiangwei
 * @since 18/1/12
 */
public class QiNiuUtil {
    //设置好账号的ACCESS_KEY和SECRET_KEY
    public static final String accessKey = "nR2-Ev-b8hhCzvlPKGs07Dkz18VKpBvT1aeTvRCr"; //这两个登录七牛 账号里面可以找到
    public static final String secretKey = "zj0UgkJr36dVWakRJFbr8zQsOUKtnwyYCyCPt8-_";

    public static final String uploadUrl = "http://up.qiniu.com";

    //要上传的空间
    public static final String bucketName = "bs-pro"; //对应要上传到七牛上 你的那个路径（自己建文件夹 注意设置公开）

    //加水印 aHR0cDovL3Fpbml1LmJpZXNodXdhbmcuY29tL3NodWl5aW4yMDMucG5n   30%
    public static final String QURL ="imageMogr2/thumbnail/2000x1500<|watermark/1/image/aHR0cDovL3Fpbml1LmJpZXNodXdhbmcuY29tL3NodWl5aW4tMjAucG5n/gravity/Center/dx/0/dy/0/ws/1|imageslim";

    //构造一个带指定Zone对象的配置类
    public static final Configuration cfg = new Configuration(Zone.zone0());
    //构建持久化数据处理对象
    public static final Auth auth = Auth.create(accessKey, secretKey);
    public static final OperationManager operationManager = new OperationManager(auth, cfg);

    /***
     * 七牛图片上传
     * @param model
     * @param redirectUrl
     * @throws IOException
     */
    public static void qiNiuUpload(Model model, String redirectUrl) throws IOException {
        model.addAttribute("token", upToken());
        model.addAttribute("uploadUrl", uploadUrl);
        model.addAttribute("storeUrl", UserUtils.storeUrl);
    }

    public static String upToken() {
        String upToken = auth.uploadToken(bucketName);
        return upToken;
    }

    public static void handleQUrl(String[]... urls) {
        if (urls == null || urls.length == 0) {
            return;
        }

        final List<String> list = new ArrayList<>();
        for (String[] item : urls) {
            if (item != null) {
                for (String u : item) {
                    list.add(u);
                }
            }
        }

        final StringMap params = new StringMap();
        params.put("notifyURL", GlobalContext.getProperties(GlobalVariableNames.BASEPATH) + "/qiniu/notify");
        params.put("force", true);
        params.put("pipeline", "bs-pro");
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (String key : list) {
                    try {
                        String persistentId = operationManager.pfop(bucketName, key, QURL, params);
                        //可以根据该 persistentId 查询任务处理进度
                        System.out.println(persistentId);
                    } catch (QiniuException e) {
                        System.err.println(e.response.toString());
                    }
                }
            }
        }).start();
    }
}
